<template>
  <el-dialog
    :title="formData.id?'编辑权限':'增加权限'"
    :visible="isShow"
    width="40%"
    @close="closeFn"
  >
    <el-form ref="form" label-width="90px" :rules="rules" :model="formData">
      <el-form-item label="权限名称" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
    </el-form>
    <template #footer class="dialog-footer">
      <el-row style="width:100%" type="flex" justify="center">
        <el-button size="small" type="primary" @click="submit">确 定</el-button>
        <el-button size="small" @click="closeFn">取 消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addPermssion, getOncePermssion, updatePerssion } from '@/api/permssion'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      formData: {
        title: '',
        code: '',
        description: '',
        pid: '0',
        type: '1'
      },
      rules: {
        title: [{ required: 'true', message: '名称必填', trigger: 'blur' }],
        code: [{ required: 'true', message: '标识必填', trigger: 'blur' }],
        description: [{ required: 'true', message: '名称必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeFn() {
      this.formData = {
        title: '',
        code: '',
        description: '',
        pid: '0',
        type: '1'
      }
      this.$emit('update:isShow', false)
      this.$refs.form.resetFields()
    },
    // 提交
    async submit() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await updatePerssion(this.formData)
      } else {
        await addPermssion(this.formData)
      }
      this.closeFn()
      this.getNewList()
    },
    // 调用父组件的列表渲染
    getNewList() {
      this.$emit('getList')
    },
    // 获取点击的权限回显
    async getOncePermssions() {
      const res = await getOncePermssion(this.formData.id)
      this.formData = res.data
    }
  }
}
</script>

<style>

</style>
